import pymysql
from config.read_config import congfig_read
import json
class mysql():
    #初始化数据库
    def __init__(self,config_path=None,sql_path=None):
        user = congfig_read(config_path).get(sql_path, 'user')
        password = congfig_read(config_path).get(sql_path, 'password')
        host = congfig_read(config_path).get(sql_path, 'host')
        port = int(congfig_read(config_path).get(sql_path, 'port'))
        database = congfig_read(config_path).get(sql_path, 'database')

        self.db=pymysql.connect(user=user,password=password,host=host,port=port,database=database,connect_timeout=3600)
        self.curser=self.db.cursor()


    def select_one(self, text):
        with self.curser as cursor:
            cursor.execute(text)
            return cursor.fetchone()


    #读取数据库全部数据
    def select_all(self,text):
        with self.curser as cursor:
            cursor.execute(text)
            data_list=[]
            data_result=self.curser.fetchall()
            for i in data_result:
                data_list.extend(i)
            return data_list

    #元组列表方式返回数据
    def all(self,text): 
        with self.curser as cursor:
            cursor.execute(text)
            data_result=self.curser.fetchall()
            return data_result
    
    
    #修改数据库数据
    def upadate(self,text):
        with self.curser as cursor:
            cursor.execute(text)
            self.db.commit()






# testcase='testcase'
# config_path = "config\YingXiao.ini"
# db_testcase = mysql(config_path,testcase)
# HomePage_text='select Interface_Name,data,request_link,header,result from t_testcase'
# HomePage_data=db_testcase.select_all(HomePage_text)
# print(type(list(HomePage_data[1])))
# print(json.loads(HomePage_data[1].strip("[]")))






